.TH TEST 1
.SH NAME \"	Copyright (C) 1989 by Kenneth Almquist.
test [ \- evaluate expressions
.SH SYNOPSIS
.B test
.I expression
.br
.B [
.I expression
.B ]
.SH DESCRIPTION
.B Test
evaluates the expression without printing the result.
The ``[''
command is a synonym for
.BR test ;
when invoked under this name
the last argument to
.B test
must be a ``]'', which is deleted and not considered part of the expression.
.PP
Three data types may occur in the
.IR expression :
string, integer, and boolean.
The rules for conversion are as follows:
.sp
.nf
\fIstring\fR\  -> \fIinteger\fR	Done via \fBatoi\fR(3).
\fIinteger\fR\ -> \fIstring\fR	Convert to decimal representation.
\fIstring\fR\  -> \fIboolean\fR	"" \-> false, everything else to true.
\fIboolean\fR\ -> \fIstring\fR	false \-> "", true \-> "true".
\fIinteger\fR\ -> \fIboolean\fR	0 \-> false, everything else to true.
\fIboolean\fR\ -> \fIinteger\fR	false \-> 0, true \-> 1.
.fi
.PP
Any argument to
.B test
which is not a legal operator is treated as a string operand of type
.BI string .
.PP
As a special case, if
.I expression
is omitted, the result is false.
.PP
We now list the operators.  The syntax
.sp
.RS
\fIinteger\fB op \fIinteger\fR \-> \fIboolean\fR (3)
.RE
.sp
means that \fBop\fR is a binary operator which takes operands of type
\fIinteger\fR and produces a result of type \fIboolean\fR.
The ``(3)'' means that the priority of \fBop\fR is 3.
Operands are automatically converted to the appropriate type.  The type
\fIany\fR is used for operator that take operands of any type.
.sp
.TP
\fIany\fR \fB-o \fIany\fR -> \fIany\fR (1)
Returns the value of the left hand operand if the left hand operand
would yield
.B true
if converted to type
.BR boolean ,
and the value of the right hand operand otherwise.
The right hand operand is evaluated only if necessary.
``|'' is a synonym for ``\-o''.
.sp
.TP
\fIany\fR \fB-a \fIany\fR -> \fIany\fR (2)
Returns the value of the left hand operand if the left hand operand
would yield
.B false
if converted to type
.BR boolean ,
and the value of the right hand operand otherwise.
The right hand operand is evaluated only if necessary.
``&'' is a synonym for ``\-a''.
.TP
\fB! \fIboolean\fR -> \fIboolean\fR (3)
Returns true if the operand is false, and false if the operand is true.
.TP
\fIstring\fR \fB= \fIstring\fR -> \fIboolean\fR (4)
True if the two strings are equal.
.TP
\fIstring\fR \fB!= \fIstring\fR -> \fIboolean\fR (4)
True if the two strings are not equal.
.TP
\fIinteger\fR \fB-eq \fIinteger\fR -> \fIboolean\fR (4)
True if the two operands are equal.
.TP
\fIinteger\fR \fB-ne \fIinteger\fR -> \fIboolean\fR (4)
True if the two operands are not equal.
.TP
\fIinteger\fR \fB-gt \fIinteger\fR -> \fIboolean\fR (4)
True if the first operand is greater than the second one.
.TP
\fIinteger\fR \fB-lt \fIinteger\fR -> \fIboolean\fR (4)
True if the first operand is less than the second one.
.TP
\fIinteger\fR \fB-ge \fIinteger\fR -> \fIboolean\fR (4)
True if the first operand is greater than or equal to the second one.
.TP
\fIinteger\fR \fB-le \fIinteger\fR -> \fIboolean\fR (5)
True if the first operand is less than or equal to the second one.
.TP
\fIinteger\fR \fB+ \fIinteger\fR -> \fIinteger\fR (6)
Add two integers.
.TP
\fIinteger\fR \fB- \fIinteger\fR -> \fIinteger\fR (6)
Subtract two integers.
.TP
\fIinteger\fR \fB* \fIinteger\fR -> \fIinteger\fR (6)
Multiply two integers.  ``*'' is special to the shell, so you generally
have to write this operator as ``\e*''.
.TP
\fIinteger\fR \fB/ \fIinteger\fR -> \fIinteger\fR (6)
Divide two integers.
.TP
\fIinteger\fR \fB% \fIinteger\fR -> \fIinteger\fR (6)
Returns the remainder when the first operand is divided by the second one.
.TP
\fIstring\fR \fB: \fIstring\fR -> \fIinteger\fR or \fIstring\fR (7)
The second operand is interpreted as a regular expression (as in the
System V
.B ed
program).
This operator attempts to match part (or all) of the first operand
with the regular expression.  The match must start at the beginning of
the first operand.
If the regular expression contains \e( \e) pairs, then the result
of this operator is the string which is matched by the regular expression
between these pairs, or the null string if no match occurred.  Otherwise,
the result is the number of characters matched by the regular expression,
or zero if no no match occurred.
.TP
\fB-n \fIstring\fR -> \fIinteger\fR (8)
Returns the number of characters in the string.
.TP
\fB-z \fIstring\fR -> \fIboolean\fR (8)
Returns true if the string contains zero characters.
.TP
\fB-t \fIinteger\fR -> \fIboolean\fR (8)
Returns true if the specified file descriptor is associated with a tty.
.PP
The remaining operators all deal with files.  Except as noted, they return
false if the
specified file does not exist.  The ones dealing with permission use
the effective user and group ids of the shell.
.TP
\fB-e \fIstring\fR \fIboolean\fR (8)
True the file exists.
.TP
\fB-r \fIstring\fR  ->  \fIboolean\fR (8)
True if you have read permission on the file.
.TP
\fB-w \fIstring\fR  ->  \fIboolean\fR (8)
True if you have write permission on the file.
.TP
\fB-x \fIstring\fR  ->  \fIboolean\fR (8)
True if you have execute permission on the file.
.TP
\fB-f \fIstring\fR  ->  \fIboolean\fR (8)
True if the file is a regular file.
.TP
\fB-d \fIstring\fR  ->  \fIboolean\fR (8)
True if the file is a directory.
.TP
\fB-c \fIstring\fR  ->  \fIboolean\fR (8)
True if the file is a character special file.
.TP
\fB-b \fIstring\fR  ->  \fIboolean\fR (8)
True if the file is a block special file.
.TP
\fB-p \fIstring\fR  ->  \fIboolean\fR (8)
True if the file is a named pipe (i.e. a fifo).
.TP
\fB-u \fIstring\fR  ->  \fIboolean\fR (8)
True if the file is setuid.
.TP
\fB-g \fIstring\fR  ->  \fIboolean\fR (8)
True if the file is setgid.
.TP
\fB-k \fIstring\fR  ->  \fIboolean\fR (8)
True if the file has the sticky bit set.
.TP
\fB-s \fIstring\fR  -> \fIinteger\fR or \fIboolean\fR (8)
Returns the size of the file, or 0 if the file does not exist.
.TP
\fB-h \fIstring\fR -> \fIboolean\fR (8)
True if the file is a symlink.  This is the only file test operator that
does not follow symlinks, all others do.  So ``\-d'' and ``\-h''
are both true on a symlink pointing to a directory.
``\-L'' is a synonym for ``\-h''.
.SH "EXIT CODE"
.TP
.I 0
If the result of 
.I expression
would be
.B true
If the result were converted to
.BR boolean .
.TP
.I 1
If the result of 
.I expression
would be
.B false
If the result were converted to
.BR boolean .
.TP
.I 2
If
.I expression
is syntactically incorrect.
.SH EXAMPLES
.TP 0.5i
filesize=`expr \-s file`
Sets the shell variable
.I filesize
to the size of
.IR file .
.TP 0.5i
if [ \-s file ]; then command; fi
Execute
.I command
if
.I file
exists and is not empty.
.TP 0.5i
x=`expr "$x" : '.\\{4\\}\\(.\\{0,3\\}\\)'`
Sets
.I x
to the substring of
.I x
beginning after the fourth character of
.I x
and continuing for three characters or until the end of the string,
whichever comes first.
.TP 0.5i
x=`expr X"$x" : X'.\\{4\\}\\(.\\{0,3\\}\\)'`
This example is the same as the previous one, but it uses a leading
``X'' to make things work when the value of
.I x
looks like an operator.
.SH BUGS
The relational operators of the System V
.B expr
command are not implemented.
.PP
Certain features of this version of
.B test
are not present in System V, so care should be used when writing
portable code.
.SH COPYRIGHT
Kenneth Almquist.
